import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Component({
  properties: {
    address: Array
  },
  data: {
    prolist: [],
    citylist: [],
    dislist: [],
    address: [],
  },

  ready: function() {
    console.log(this.data.address, 'address-ready')

    if (this.data.address.length ==3) {
      this.getProList()
      this.getCityList(this.data.address[0].code)
      this.getDisList(this.data.address[1].code)
      this.setData({
        select1: this.data.address[0].code || '',
        select2: this.data.address[1].code || '',
        select3: this.data.address[2].code || '',
        prodata: this.data.address[0],
        citydata: this.data.address[1],
        disdata: this.data.address[2]
      })
    } else if (this.data.address.length ==2) {
      this.getProList()
      this.getCityList(this.data.address[0].code) 
      this.setData({
        select1: this.data.address[0].code || '',
        select2: this.data.address[1].code || '',
        prodata: this.data.address[0],
        citydata: this.data.address[1]
      })
    } else if (this.data.address.length == 1) {
      this.getProList() 
      this.setData({
        select1: this.data.address[0].code || '', 
        prodata: this.data.address[0]
      })
    } else {
      this.getProList(); 
    }



  },



  methods: {
    //获取省份列表
    getProList: function() {
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: 'POST',
        loading: false,
        data: {
          "pid": 0,
          "pageNo": 1,
          "pageSize": 100
        }
      }, (res) => {
        this.setData({
          prolist: res.list
        })
      })
    },

    selectpro: function(e) {
      var code = e.currentTarget.dataset.code;
      var item = e.currentTarget.dataset.item;

      this.setData({
        prodata: {
          code: item.code,
          name: item.name
        },
        select1: item.code,
        dislist: [],
        citydata: null,
        disdata: null
      })
      this.addressData()
      this.getCityList(code)
    },


    //获取城市
    getCityList: function(code) {
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: 'POST',
        loading: false,
        data: {
          "pid": code,
          "pageNo": 1,
          "pageSize": 100
        }
      }, (res) => {
        this.setData({
          citylist: res.list
        })

      })
    },

    selectcity: function(e) {
      var code = e.currentTarget.dataset.code;
      var item = e.currentTarget.dataset.item;

      this.setData({
        disdata: null,
        select2: item.code,
        citydata: {
          code: item.code,
          name: item.name
        },
      })
      this.addressData()
      this.getDisList(code)
    },


    //区域
    getDisList: function(code) {
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: 'POST',
        loading: false,
        data: {
          "pid": code,
          "pageNo": 1,
          "pageSize": 100
        }
      }, (res) => {
        this.setData({
          dislist: res.list
        })
      })
    },

    selectdis: function(e) {
      var code = e.currentTarget.dataset.code;
      var item = e.currentTarget.dataset.item;
      this.setData({
        select3: item.code,
        disdata: {
          code: item.code,
          name: item.name
        },
      })
      this.addressData()
    },


    //获取地址信息
    addressData: function() {
      var address = [this.data.prodata, this.data.citydata, this.data.disdata]
      address = address.filter(item => !!item)
      console.log(address,'addressData')
      this.setData({
        address
      })
    },


    //重置
    quyuEmpty: function() {
      this.getProList(),
        this.setData({
          prodata: null,
          citydata: null,
          disdata: null,
          select1: '',
          select2: '',
          select3: '',
          dislist: [],
          citylist: []
        })
    },


    submitFilter: function() {
      this.triggerEvent("addressDetail", this.data.address)
    },


    cancel: function() {
      this.triggerEvent("addresscancel", false)
    }



  }
})